package wcg;

import ikelib.NumericTextField;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Dimension;
import javax.swing.JSlider;
import java.awt.Font;
import javax.swing.JCheckBox;

import GraphicView.GraphicView;

/**
 * マニュアルスケールダイアログ　１つのCHの振幅、オフセット、センター値を編集出来る。
 * @author Toshiharu Ikeda <ikeda.104@gmail.com>
 *
 */
public class DialogYFixScale extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	public JButton jButtonOK = null;
	private NumericTextField jTextFieldCenter = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private NumericTextField jTextFieldAmplitude = null;
	/**
	 * センター値　常にdigitに変換されている
	 */
	public int CenterValue;
	/**
	 * 振幅値　常にdigitに変換されている
	 */
	public long AmplitudeValue;
	/**
	 * オフセット値　常にdigitに変換されている
	 */
	public int OffsetValue;
	/**
	 * 振幅を全ｃｈに適用フラグ
	 */
	public boolean Set2AllChannelAmp=false;
	/**
	 * センター値を全CHに適用フラグ
	 */
	public boolean Set2AllChannelCenter=false;
	/**
	 * action performedの呼び出し先
	 */
	private  GraphicView Parent;	// @
	public int channel;
	public double Factor;
	public String Unit = "";  //  @jve:decl-index=0:
	public long FullScale = 1000;
	private long MaxValue;
	private long MinValue;
	public int UnitType = 0; // 0: digit 1:phycicalunit
	private JButton jButtonUp = null;
	private JButton jButtonDown = null;
	private JButton jButtonAmpUp = null;
	private JButton jButtonAmpDown = null;
	private NumericTextField jTextFieldOffset = null;
	private JLabel jLabelOffset = null;
	private JButton jButtonGetCenter = null;
	private JLabel jLabelUnit = null;
	private JLabel jLabelUnit2 = null;
	private JLabel jLabelUnit3 = null;
	private JLabel jLabelInfo = null;
	private JButton jButtonApply = null;
	private JCheckBox jCheckBoxSetSigleAmp = null;
	private JCheckBox jCheckBoxSetSigleCenter = null;
	/**
	 * @param owner
	 */
	public DialogYFixScale(Frame owner) {
		super(owner);
		initialize();
	}
	public void setParent(GraphicView owner) {
		Parent = owner;		// @
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(242, 374);
		this.setTitle("Y scale");
		this.setContentPane(getJContentPane());
		
	}
	public void setparam(){
		MaxValue = FullScale/2-1;
		MinValue = -FullScale/2;
		if(UnitType==ChannelDataDisplayProperty.UNIT_TYPE_DIGIT){
			jTextFieldCenter.setText(String.format("%,d", CenterValue));
			jTextFieldAmplitude.setText(String.format("%,d", AmplitudeValue));
			jTextFieldOffset.setText(String.format("%,d", OffsetValue));
			jLabelUnit.setText("");
			jLabelUnit2.setText("");
			jLabelUnit3.setText("");
			jLabelInfo.setText(String.format("Max=%d Min=%d",MaxValue,MinValue));
		}
		else if(UnitType==ChannelDataDisplayProperty.UNIT_TYPE_CONV){
			jTextFieldCenter.setText(String.format("%g", (double)CenterValue*Factor));
			jTextFieldAmplitude.setText(String.format("%g", (double)AmplitudeValue*Factor));
			jTextFieldOffset.setText(String.format("%g", (double)OffsetValue*Factor));
			jLabelUnit.setText(Unit);
			jLabelUnit2.setText(Unit);
			jLabelUnit3.setText(Unit);
			jLabelInfo.setText(String.format("Max=%g Min=%g",Factor*(MaxValue),Factor*(MinValue)));
		}
		
		String title = new String();
		title = String.format("YFixScale=%04X", channel);
		setTitle(title);
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelInfo = new JLabel();
			jLabelInfo.setBounds(new Rectangle(10, 5, 213, 22));  // Generated
			jLabelInfo.setText("JLabel");  // Generated
			jLabelUnit3 = new JLabel();
			jLabelUnit3.setBounds(new Rectangle(160, 236, 63, 22));  // Generated
			jLabelUnit3.setText("");  // Generated
			jLabelUnit2 = new JLabel();
			jLabelUnit2.setBounds(new Rectangle(159, 147, 65, 21));  // Generated
			jLabelUnit2.setText("");  // Generated
			jLabelUnit = new JLabel();
			jLabelUnit.setBounds(new Rectangle(157, 63, 65, 22));  // Generated
			jLabelUnit.setText("");  // Generated
			jLabelOffset = new JLabel();
			jLabelOffset.setBounds(new Rectangle(9, 119, 53, 23));
			jLabelOffset.setText("Offset");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(11, 207, 80, 25));
			jLabel2.setText("Amplitude");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(8, 36, 56, 25));
			jLabel1.setText("Center");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJButtonOK(), null);
			jContentPane.add(getJTextFieldCenter(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getJTextFieldAmplitude(), null);
			jContentPane.add(getJButtonUp(), null);
			jContentPane.add(getJButtonDown(), null);
			jContentPane.add(getJButtonAmpUp(), null);
			jContentPane.add(getJButtonAmpDown(), null);
			jContentPane.add(getJTextFieldOffset(), null);
			jContentPane.add(jLabelOffset, null);
			jContentPane.add(getJButtonGetCenter(), null);
			jContentPane.add(jLabelUnit, null);  // Generated
			jContentPane.add(jLabelUnit2, null);  // Generated
			jContentPane.add(jLabelUnit3, null);  // Generated
			jContentPane.add(jLabelInfo, null);  // Generated
			jContentPane.add(getJButtonApply(), null);  // Generated
			jContentPane.add(getJCheckBoxSetSigleAmp(), null);  // Generated
			jContentPane.add(getJCheckBoxSetSigleCenter(), null);
		}
		return jContentPane;
	}
	private void getData(){
		try{
			Set2AllChannelAmp = jCheckBoxSetSigleAmp.isSelected();
			Set2AllChannelCenter = jCheckBoxSetSigleCenter.isSelected();
			if(UnitType==ChannelDataDisplayProperty.UNIT_TYPE_DIGIT){
				String cv = new String(jTextFieldCenter.getText());
				CenterValue = Integer.parseInt(cv);
				cv = new String(jTextFieldAmplitude.getText());
				AmplitudeValue = Integer.parseInt(cv);
				cv = new String(jTextFieldOffset.getText());
				OffsetValue = Integer.parseInt(cv);
			}
			else if(UnitType==ChannelDataDisplayProperty.UNIT_TYPE_CONV){
				CenterValue = (int)(Double.parseDouble(jTextFieldCenter.getText())/Factor);
				AmplitudeValue = (int)(Double.parseDouble(jTextFieldAmplitude.getText())/Factor);
				OffsetValue = (int)(Double.parseDouble(jTextFieldOffset.getText())/Factor);
			}
		} catch(NumberFormatException e){
			e.printStackTrace();
		}
	}
	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setBounds(new Rectangle(131, 303, 89, 26));
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData();
					Parent.actionPerformed(e);
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jTextFieldCenter	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCenter() {
		if (jTextFieldCenter == null) {
			jTextFieldCenter = new NumericTextField();
			jTextFieldCenter.setBounds(new Rectangle(10, 62, 145, 24));
		}
		return jTextFieldCenter;
	}

	/**
	 * This method initializes jTextFieldAmplitude	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldAmplitude() {
		if (jTextFieldAmplitude == null) {
			jTextFieldAmplitude = new NumericTextField();
			jTextFieldAmplitude.setBounds(new Rectangle(10, 235, 149, 24));
		}
		return jTextFieldAmplitude;
	}
	/**
	 * This method initializes jButtonUp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonUp() {
		if (jButtonUp == null) {
			//jButtonUp = new JButton();
			jButtonUp = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/arrow_up.png")));
			jButtonUp.setBounds(new Rectangle(99, 35, 25, 25));
			
			jButtonUp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData();
					if((long)CenterValue-MinValue > (long)AmplitudeValue/5){
						CenterValue -= AmplitudeValue/5;
					}
					else{
						CenterValue=(int)MinValue;
					}
					setparam();
					Parent.actionPerformed(e);
				}
			});
		}
		return jButtonUp;
	}
	/**
	 * This method initializes jButtonDown	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDown() {
		if (jButtonDown == null) {
			jButtonDown = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/arrow_down.png")));
			jButtonDown.setBounds(new Rectangle(128, 35, 25, 25));
			jButtonDown.setPreferredSize(new Dimension(45, 20));
		
			jButtonDown.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData();
					if(MaxValue-(long)CenterValue-1 > (long)AmplitudeValue/5){
						CenterValue += AmplitudeValue/5;
					}
					else{
						CenterValue = (int)MaxValue;
					}
					setparam();
					Parent.actionPerformed(e);
				}
			});
		}
		return jButtonDown;
	}
	/**
	 * This method initializes jButtonAmpUp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAmpUp() {
		if (jButtonAmpUp == null) {
			jButtonAmpUp = new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/loupe_plus_16.png")));
			jButtonAmpUp.setBounds(new Rectangle(104, 206, 25, 25));
			jButtonAmpUp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData();
					if(AmplitudeValue>5){
						AmplitudeValue = AmplitudeValue*4/5;
					}
					setparam();
					Parent.actionPerformed(e);
				}
			});
		}
		return jButtonAmpUp;
	}
	/**
	 * This method initializes jButtonAmpDown	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAmpDown() {
		if (jButtonAmpDown == null) {
			jButtonAmpDown =  new JButton(new ImageIcon(getClass().getClassLoader().getResource("images/loupe_minus_16.png")));
			jButtonAmpDown.setBounds(new Rectangle(132, 206, 25, 25));
			jButtonAmpDown.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData();
					if(AmplitudeValue<MaxValue/5*4){
						if(AmplitudeValue<20) AmplitudeValue=20;
						AmplitudeValue /= 4;
						AmplitudeValue *= 5;
					}
					else{
						AmplitudeValue=(int)MaxValue;
					}
					setparam();
					Parent.actionPerformed(e);
				}
			});
		}
		return jButtonAmpDown;
	}
	/**
	 * This method initializes jTextFieldOffset	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldOffset() {
		if (jTextFieldOffset == null) {
			jTextFieldOffset = new NumericTextField();
			jTextFieldOffset.setBounds(new Rectangle(9, 146, 147, 24));
		}
		return jTextFieldOffset;
	}
	/**
	 * This method initializes jButtonGetCenter	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonGetCenter() {
		if (jButtonGetCenter == null) {
			jButtonGetCenter = new JButton();
			jButtonGetCenter.setBounds(new Rectangle(70, 123, 133, 19));
			jButtonGetCenter.setText("Get from Center");
			jButtonGetCenter.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jTextFieldOffset.setText(jTextFieldCenter.getText());
				}
			});
		}
		return jButtonGetCenter;
	}
	/**
	 * This method initializes jButtonApply	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonApply() {
		if (jButtonApply == null) {
			jButtonApply = new JButton();
			jButtonApply.setBounds(new Rectangle(24, 302, 90, 26));  // Generated
			jButtonApply.setText("Apply");  // Generated
			jButtonApply.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getData();
					Parent.actionPerformed(e);				
				}
			});
		}
		return jButtonApply;
	}
	/**
	 * This method initializes jCheckBoxSetSigleAmp	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxSetSigleAmp() {
		if (jCheckBoxSetSigleAmp == null) {
			jCheckBoxSetSigleAmp = new JCheckBox();
			jCheckBoxSetSigleAmp.setBounds(new Rectangle(9, 262, 214, 19));  // Generated
			jCheckBoxSetSigleAmp.setText("Set this amplitude to all channel ");  // Generated
		}
		return jCheckBoxSetSigleAmp;
	}
	/**
	 * This method initializes jCheckBoxSetSigleCenter	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getJCheckBoxSetSigleCenter() {
		if (jCheckBoxSetSigleCenter == null) {
			jCheckBoxSetSigleCenter = new JCheckBox();
			jCheckBoxSetSigleCenter.setBounds(new Rectangle(7, 86, 208, 24));
			jCheckBoxSetSigleCenter.setText("Set this center to all channel ");
		}
		return jCheckBoxSetSigleCenter;
	}

}  //  @jve:decl-index=0:visual-constraint="82,10"
